home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / ncurses-5.3.lha / ncurses-5.3 / Ada95 / samples / ncurses2-trace_set.adb < prev    next >
Text File  |  2002-10-24  |  17KB  |  482 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                       GNAT ncurses Binding Samples                       --
  4. --                                                                          --
  5. --                            ncurses2.trace_set                            --
  6. --                                                                          --
  7. --                                 B O D Y                                  --
  8. --                                                                          --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 2000 Free Software Foundation, Inc.                        --
  11. --                                                                          --
  12. -- Permission is hereby granted, free of charge, to any person obtaining a  --
  13. -- copy of this software and associated documentation files (the            --
  14. -- "Software"), to deal in the Software without restriction, including      --
  15. -- without limitation the rights to use, copy, modify, merge, publish,      --
  16. -- distribute, distribute with modifications, sublicense, and/or sell       --
  17. -- copies of the Software, and to permit persons to whom the Software is    --
  18. -- furnished to do so, subject to the following conditions:                 --
  19. --                                                                          --
  20. -- The above copyright notice and this permission notice shall be included  --
  21. -- in all copies or substantial portions of the Software.                   --
  22. --                                                                          --
  23. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  --
  24. -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               --
  25. -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   --
  26. -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   --
  27. -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    --
  28. -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    --
  29. -- THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               --
  30. --                                                                          --
  31. -- Except as contained in this notice, the name(s) of the above copyright   --
  32. -- holders shall not be used in advertising or otherwise to promote the     --
  33. -- sale, use or other dealings in this Software without prior written       --
  34. -- authorization.                                                           --
  35. ------------------------------------------------------------------------------
  36. --  Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
  37. --  Version Control
  38. --  $Revision: 1.1 $
  39. --  Binding Version 01.00
  40. ------------------------------------------------------------------------------
  41. with ncurses2.util; use ncurses2.util;
  42. with Terminal_Interface.Curses; use Terminal_Interface.Curses;
  43. with Terminal_Interface.Curses.Trace; use Terminal_Interface.Curses.Trace;
  44. with Terminal_Interface.Curses.Menus; use Terminal_Interface.Curses.Menus;
  45.  
  46. with Ada.Strings.Bounded;
  47.  
  48. --  interactively set the trace level
  49.  
  50. procedure ncurses2.trace_set is
  51.  
  52.    function menu_virtualize (c : Key_Code) return Menu_Request_Code;
  53.    function subset (super, sub : Trace_Attribute_Set) return Boolean;
  54.    function trace_or (a, b : Trace_Attribute_Set) return Trace_Attribute_Set;
  55.    function trace_num (tlevel : Trace_Attribute_Set) return String;
  56.    function tracetrace (tlevel : Trace_Attribute_Set) return String;
  57.    function run_trace_menu (m : Menu) return Boolean;
  58.  
  59.    function menu_virtualize (c : Key_Code) return Menu_Request_Code is
  60.    begin
  61.       case c is
  62.          when Character'Pos (newl) | Key_Exit =>
  63.             return Menu_Request_Code'Last + 1; --  MAX_COMMAND? TODO
  64.          when Character'Pos ('u') =>
  65.             return M_ScrollUp_Line;
  66.          when Character'Pos ('d') =>
  67.             return M_ScrollDown_Line;
  68.          when Character'Pos ('b') | Key_Next_Page =>
  69.             return M_ScrollUp_Page;
  70.          when Character'Pos ('f') | Key_Previous_Page =>
  71.             return M_ScrollDown_Page;
  72.          when Character'Pos ('n') | Key_Cursor_Down =>
  73.             return M_Next_Item;
  74.          when Character'Pos ('p') | Key_Cursor_Up =>
  75.             return M_Previous_Item;
  76.          when Character'Pos (' ') =>
  77.             return M_Toggle_Item;
  78.          when Key_Mouse =>
  79.             return c;
  80.          when others =>
  81.             Beep;
  82.             return c;
  83.       end case;
  84.    end menu_virtualize;
  85.  
  86.  
  87.    type string_a is access String;
  88.    type tbl_entry is record
  89.       name : string_a;
  90.       mask : Trace_Attribute_Set;
  91.    end record;
  92.  
  93.    t_tbl : constant array (Positive range <>) of tbl_entry :=
  94.      (
  95.       (new String'("Disable"),
  96.        Trace_Disable),
  97.       (new String'("Times"),
  98.        Trace_Attribute_Set'(Times => True, others => False)),
  99.       (new String'("Tputs"),
  100.        Trace_Attribute_Set'(Tputs => True, others => False)),
  101.       (new String'("Update"),
  102.        Trace_Attribute_Set'(Update => True, others => False)),
  103.       (new String'("Cursor_Move"),
  104.        Trace_Attribute_Set'(Cursor_Move => True, others => False)),
  105.       (new String'("Character_Output"),
  106.        Trace_Attribute_Set'(Character_Output => True, others => False)),
  107.       (new String'("Ordinary"),
  108.        Trace_Ordinary),
  109.       (new String'("Calls"),
  110.        Trace_Attribute_Set'(Calls => True, others => False)),
  111.       (new String'("Virtual_Puts"),
  112.        Trace_Attribute_Set'(Virtual_Puts => True, others => False)),
  113.       (new String'("Input_Events"),
  114.        Trace_Attribute_Set'(Input_Events => True, others => False)),
  115.       (new String'("TTY_State"),
  116.        Trace_Attribute_Set'(TTY_State => True, others => False)),
  117.       (new String'("Internal_Calls"),
  118.        Trace_Attribute_Set'(Internal_Calls => True, others => False)),
  119.       (new String'("Character_Calls"),
  120.        Trace_Attribute_Set'(Character_Calls => True, others => False)),
  121.       (new String'("Termcap_TermInfo"),
  122.        Trace_Attribute_Set'(Termcap_TermInfo => True, others => False)),
  123.       (new String'("Maximium"),
  124.        Trace_Maximum)
  125.       );
  126.  
  127.    package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (300);
  128.  
  129.  
  130.    function subset (super, sub : Trace_Attribute_Set) return Boolean is
  131.    begin
  132.       if
  133.         (super.Times or not sub.Times) and
  134.         (super.Tputs or not sub.Tputs) and
  135.         (super.Update or not sub.Update) and
  136.         (super.Cursor_Move or not sub.Cursor_Move) and
  137.         (super.Character_Output or not sub.Character_Output) and
  138.         (super.Calls or not sub.Calls) and
  139.         (super.Virtual_Puts or not sub.Virtual_Puts) and
  140.         (super.Input_Events or not sub.Input_Events) and
  141.         (super.TTY_State or not sub.TTY_State) and
  142.         (super.Internal_Calls or not sub.Internal_Calls) and
  143.         (super.Character_Calls or not sub.Character_Calls) and
  144.         (super.Termcap_TermInfo or not sub.Termcap_TermInfo) and
  145.         True then
  146.          return True;
  147.       else
  148.          return False;
  149.       end if;
  150.    end subset;
  151.  
  152.    function trace_or (a, b : Trace_Attribute_Set) return Trace_Attribute_Set is
  153.       retval : Trace_Attribute_Set := Trace_Disable;
  154.    begin
  155.       retval.Times := (a.Times or b.Times);
  156.       retval.Tputs := (a.Tputs or b.Tputs);
  157.       retval.Update := (a.Update or b.Update);
  158.       retval.Cursor_Move := (a.Cursor_Move or b.Cursor_Move);
  159.       retval.Character_Output := (a.Character_Output or b.Character_Output);
  160.       retval.Calls := (a.Calls or b.Calls);
  161.       retval.Virtual_Puts := (a.Virtual_Puts or b.Virtual_Puts);
  162.       retval.Input_Events := (a.Input_Events or b.Input_Events);
  163.       retval.TTY_State := (a.TTY_State or b.TTY_State);
  164.       retval.Internal_Calls := (a.Internal_Calls or b.Internal_Calls);
  165.       retval.Character_Calls := (a.Character_Calls or b.Character_Calls);
  166.       retval.Termcap_TermInfo := (a.Termcap_TermInfo or b.Termcap_TermInfo);
  167.  
  168.       return retval;
  169.    end trace_or;
  170.  
  171.    --  Print the hexadecimal value of the mask so
  172.    --  users can set it from the command line.
  173.  
  174.    function trace_num (tlevel : Trace_Attribute_Set) return String is
  175.       result : Integer := 0;
  176.       m : Integer := 1;
  177.    begin
  178.  
  179.       if tlevel.Times then
  180.          result := result + m;
  181.       end if;
  182.       m := m * 2;
  183.  
  184.       if tlevel.Tputs then
  185.          result := result + m;
  186.       end if;
  187.       m := m * 2;
  188.  
  189.       if tlevel.Update then
  190.          result := result + m;
  191.       end if;
  192.       m := m * 2;
  193.  
  194.       if tlevel.Cursor_Move then
  195.          result := result + m;
  196.       end if;
  197.       m := m * 2;
  198.  
  199.       if tlevel.Character_Output then
  200.          result := result + m;
  201.       end if;
  202.       m := m * 2;
  203.  
  204.       if tlevel.Calls then
  205.          result := result + m;
  206.       end if;
  207.       m := m * 2;
  208.  
  209.       if tlevel.Virtual_Puts then
  210.          result := result + m;
  211.       end if;
  212.       m := m * 2;
  213.  
  214.       if tlevel.Input_Events then
  215.          result := result + m;
  216.       end if;
  217.       m := m * 2;
  218.  
  219.       if tlevel.TTY_State then
  220.          result := result + m;
  221.       end if;
  222.       m := m * 2;
  223.  
  224.       if tlevel.Internal_Calls then
  225.          result := result + m;
  226.       end if;
  227.       m := m * 2;
  228.  
  229.       if tlevel.Character_Calls then
  230.          result := result + m;
  231.       end if;
  232.       m := m * 2;
  233.  
  234.       if tlevel.Termcap_TermInfo then
  235.          result := result + m;
  236.       end if;
  237.       m := m * 2;
  238.       return result'Img;
  239.    end trace_num;
  240.  
  241.  
  242.    function tracetrace (tlevel : Trace_Attribute_Set) return String is
  243.  
  244.       use BS;
  245.       buf : Bounded_String := To_Bounded_String ("");
  246.    begin
  247.       --  The C version prints the hexadecimal value of the mask, we
  248.       --  won't do that here because this is Ada.
  249.  
  250.       if tlevel = Trace_Disable then
  251.          Append (buf, "Trace_Disable");
  252.       else
  253.  
  254.  
  255.          if subset (tlevel,
  256.                     Trace_Attribute_Set'(Times => True, others => False)) then
  257.             Append (buf, "Times");
  258.             Append (buf, ", ");
  259.          end if;
  260.  
  261.          if subset (tlevel,
  262.                     Trace_Attribute_Set'(Tputs => True, others => False)) then
  263.             Append (buf, "Tputs");
  264.             Append (buf, ", ");
  265.          end if;
  266.  
  267.          if subset (tlevel,
  268.                     Trace_Attribute_Set'(Update => True, others => False)) then
  269.             Append (buf, "Update");
  270.             Append (buf, ", ");
  271.          end if;
  272.  
  273.          if subset (tlevel,
  274.                     Trace_Attribute_Set'(Cursor_Move => True,
  275.                                          others => False)) then
  276.             Append (buf, "Cursor_Move");
  277.             Append (buf, ", ");
  278.          end if;
  279.  
  280.          if subset (tlevel,
  281.                     Trace_Attribute_Set'(Character_Output => True,
  282.                                          others => False)) then
  283.             Append (buf, "Character_Output");
  284.             Append (buf, ", ");
  285.          end if;
  286.  
  287.          if subset (tlevel,
  288.                     Trace_Ordinary) then
  289.             Append (buf, "Ordinary");
  290.             Append (buf, ", ");
  291.          end if;
  292.  
  293.          if subset (tlevel,
  294.                     Trace_Attribute_Set'(Calls => True, others => False)) then
  295.             Append (buf, "Calls");
  296.             Append (buf, ", ");
  297.          end if;
  298.  
  299.          if subset (tlevel,
  300.                     Trace_Attribute_Set'(Virtual_Puts => True,
  301.                                          others => False)) then
  302.             Append (buf, "Virtual_Puts");
  303.             Append (buf, ", ");
  304.          end if;
  305.  
  306.          if subset (tlevel,
  307.                     Trace_Attribute_Set'(Input_Events => True,
  308.                                          others => False)) then
  309.             Append (buf, "Input_Events");
  310.             Append (buf, ", ");
  311.          end if;
  312.  
  313.          if subset (tlevel,
  314.                     Trace_Attribute_Set'(TTY_State => True,
  315.                                          others => False)) then
  316.             Append (buf, "TTY_State");
  317.             Append (buf, ", ");
  318.          end if;
  319.  
  320.          if subset (tlevel,
  321.                     Trace_Attribute_Set'(Internal_Calls => True,
  322.                                          others => False)) then
  323.             Append (buf, "Internal_Calls");
  324.             Append (buf, ", ");
  325.          end if;
  326.  
  327.          if subset (tlevel,
  328.                     Trace_Attribute_Set'(Character_Calls => True,
  329.                                          others => False)) then
  330.             Append (buf, "Character_Calls");
  331.             Append (buf, ", ");
  332.          end if;
  333.  
  334.          if subset (tlevel,
  335.                     Trace_Attribute_Set'(Termcap_TermInfo => True,
  336.                                          others => False)) then
  337.             Append (buf, "Termcap_TermInfo");
  338.             Append (buf, ", ");
  339.          end if;
  340.  
  341.          if subset (tlevel,
  342.                     Trace_Maximum) then
  343.             Append (buf, "Maximium");
  344.             Append (buf, ", ");
  345.          end if;
  346.       end if;
  347.  
  348.       if To_String (buf) (Length (buf) - 1) = ',' then
  349.          Delete (buf, Length (buf) - 1, Length (buf));
  350.       end if;
  351.  
  352.       return To_String (buf);
  353.    end tracetrace;
  354.  
  355.    function run_trace_menu (m : Menu) return Boolean is
  356.       i, p : Item;
  357.       changed : Boolean;
  358.       c, v : Key_Code;
  359.    begin
  360.       loop
  361.          changed := False;
  362.          c := Getchar (Get_Window (m));
  363.          v := menu_virtualize (c);
  364.          case Driver (m, v) is
  365.             when Unknown_Request =>
  366.                return False;
  367.             when others =>
  368.                i := Current (m);
  369.                if i = Menus.Items (m, 1) then -- the first item
  370.                   for n in t_tbl'First + 1 .. t_tbl'Last loop
  371.                      if Value (i) then
  372.                         Set_Value (i, False);
  373.                         changed := True;
  374.                      end if;
  375.                   end loop;
  376.                else
  377.                   for n in t_tbl'First + 1 .. t_tbl'Last loop
  378.                      p := Menus.Items (m, n);
  379.                      if Value (p) then
  380.                         Set_Value (Menus.Items (m, 1), False);
  381.                         changed := True;
  382.                         exit;
  383.                      end if;
  384.                   end loop;
  385.                end if;
  386.                if not changed then
  387.                   return True;
  388.                end if;
  389.          end case;
  390.       end loop;
  391.    end run_trace_menu;
  392.  
  393.    nc_tracing, mask : Trace_Attribute_Set;
  394.    pragma Import (C, nc_tracing, "_nc_tracing");
  395.    items_a : Item_Array_Access :=
  396.      new Item_Array (t_tbl'First .. t_tbl'Last + 1);
  397.    mrows : Line_Count;
  398.    mcols : Column_Count;
  399.    menuwin : Window;
  400.    menu_y : constant Line_Position := 8;
  401.    menu_x : constant Column_Position := 8;
  402.    ip : Item;
  403.    m : Menu;
  404.    newtrace : Trace_Attribute_Set;
  405. begin
  406.    Add (Line => 0, Column => 0, Str => "Interactively set trace level:");
  407.    Add (Line => 2, Column => 0,
  408.         Str => "  Press space bar to toggle a selection.");
  409.    Add (Line => 3, Column => 0,
  410.         Str => "  Use up and down arrow to move the select bar.");
  411.    Add (Line => 4, Column => 0,
  412.         Str => "  Press return to set the trace level.");
  413.    Add (Line => 6, Column => 0, Str => "(Current trace level is ");
  414.    Add (Str => tracetrace (nc_tracing) & " numerically: " &
  415.         trace_num (nc_tracing));
  416.    Add (Ch => ')');
  417.  
  418.    Refresh;
  419.  
  420.    for n in t_tbl'Range loop
  421.       items_a (n) := New_Item (t_tbl (n).name.all);
  422.    end loop;
  423.    items_a (t_tbl'Last + 1) := Null_Item;
  424.  
  425.    m := New_Menu (items_a);
  426.  
  427.    Set_Format (m, 16, 2);
  428.    Scale (m, mrows, mcols);
  429.  
  430.    Switch_Options (m, (One_Valued => True, others => False), On => False);
  431.    menuwin := New_Window (mrows + 2, mcols + 2, menu_y, menu_x);
  432.    Set_Window (m, menuwin);
  433.    Set_KeyPad_Mode (menuwin, SwitchOn => True);
  434.    Box (menuwin);
  435.  
  436.    Set_Sub_Window (m, Derived_Window (menuwin, mrows, mcols, 1, 1));
  437.  
  438.    Post (m);
  439.  
  440.    for n in t_tbl'Range loop
  441.       ip := Items (m, n);
  442.       mask := t_tbl (n).mask;
  443.       if mask = Trace_Disable then
  444.          Set_Value (ip, nc_tracing = Trace_Disable);
  445.       elsif subset (sub => mask, super => nc_tracing) then
  446.          Set_Value (ip, True);
  447.       end if;
  448.    end loop;
  449.  
  450.    while run_trace_menu (m) loop
  451.       null;
  452.    end loop;
  453.  
  454.    newtrace := Trace_Disable;
  455.    for n in t_tbl'Range loop
  456.       ip := Items (m, n);
  457.       if Value (ip) then
  458.          mask := t_tbl (n).mask;
  459.          newtrace := trace_or (newtrace, mask);
  460.       end if;
  461.    end loop;
  462.  
  463.    Trace_On (newtrace);
  464.    Trace_Put ("trace level interactively set to " &
  465.               tracetrace (nc_tracing));
  466.  
  467.    Move_Cursor (Line => Lines - 4, Column => 0);
  468.    Add (Str => "Trace level is ");
  469.    Add (Str => tracetrace (nc_tracing));
  470.    Add (Ch => newl);
  471.    Pause; -- was just Add(); Getchar
  472.  
  473.    Post (m, False);
  474.    --  menuwin has subwindows I think, which makes an error.
  475.    declare begin
  476.       Delete (menuwin);
  477.    exception when Curses_Exception => null; end;
  478.  
  479.    --  free_menu(m);
  480.    --  free_item()
  481. end ncurses2.trace_set;
  482.